﻿using System;
using System.Collections.Generic;
using System.Linq;
using studentSubsidy.Dao;
using studentSubsidy.Entity;
using studentSubsidy.Entity.AdminProject;
using studentSubsidy.Models;
using studentSubsidy.Utils;

namespace studentSubsidy.Service
{
    public class AdminProjectService
    {
        private AdminProjectDao _adminProjectDao = new AdminProjectDao();

        public Result getProjectList(string role)
        {
            List<Project> allProjects = _adminProjectDao.getProjectList(role);
            List<AdminProjectReturn> adminProjectReturns = new List<AdminProjectReturn>();
            foreach (var VARIABLE in allProjects)
            {
                AdminProjectReturn adminProjectReturn = new AdminProjectReturn
                {
                    product_id = VARIABLE.product_id,
                    product_intro = VARIABLE.product_intro,
                    product_name = VARIABLE.product_name,
                    product_price = VARIABLE.product_price,
                    product_title = VARIABLE.product_title
                };
                if (VARIABLE.category_id == 1)
                {
                    adminProjectReturn.product_category = "校级奖学金";
                }

                if (VARIABLE.category_id == 2)
                {
                    adminProjectReturn.product_category = "市级奖学金";
                }

                if (VARIABLE.category_id == 3)
                {
                    adminProjectReturn.product_category = "校级助学金";
                }

                if (VARIABLE.category_id == 4)
                {
                    adminProjectReturn.product_category = "市级助学金";
                }

                adminProjectReturns.Add(adminProjectReturn);
            }

            return new Result(200, "搜索成功", adminProjectReturns, null);
        }

        public Result addProject(AddProjectQuery addProjectQuery)
        {
            Project project = new Project();
            project.product_id = addProjectQuery.product_id;
            project.product_name = addProjectQuery.product_name;
            project.product_title = null;
            project.product_picture = addProjectQuery.product_image.Replace("http://localhost:99/", "");
            project.product_price = addProjectQuery.product_price;
            project.product_notice = addProjectQuery.product_notice;

            foreach (var VARIABLE in addProjectQuery.product_title)
            {
                if (VARIABLE == "")
                {
                    continue;
                }
                project.product_title =  VARIABLE+","+project.product_title;
            }
            if (addProjectQuery.product_refund)
            {
                project.isRefund = 1;
            }
            else
            {
                project.isRefund = 0;
            }

            if (addProjectQuery.product_category == "奖学金" && addProjectQuery.role == "school")
            {
                project.category_id = 1;
            }

            if (addProjectQuery.product_category == "奖学金" && addProjectQuery.role == "bureau")
            {
                project.category_id = 2;
            }

            if (addProjectQuery.product_category == "助学金" && addProjectQuery.role == "school")
            {
                project.category_id = 3;
            }

            if (addProjectQuery.product_category == "助学金" && addProjectQuery.role == "bureau")
            {
                project.category_id = 4;
            }

            _adminProjectDao.addProject(project);
            return new Result(200, "添加成功", null, null);
        }

        public Result getProjectInfo(int product_id)
        {
            Project project = _adminProjectDao.getProjectById(product_id);
            UpdateProjectReturn updateProjectReturn = new UpdateProjectReturn();
            updateProjectReturn.product_name = project.product_name;
            updateProjectReturn.product_intro = project.product_intro;
            updateProjectReturn.product_price = project.product_price;
            updateProjectReturn.product_id = project.product_id;
            updateProjectReturn.product_picture = project.product_picture;
            updateProjectReturn.product_notice = project.product_notice;
            if (project.category_id==1||project.category_id==2)
            {
                updateProjectReturn.product_category = "奖学金";
            }
            if (project.category_id==3||project.category_id==4)
            {
                updateProjectReturn.product_category = "助学金";
            }

            if (project.isRefund == 0)
            {
                updateProjectReturn.product_refund = false;
            }
            else
            {
                updateProjectReturn.product_refund = true;
            }

            List<string> product_title = project.product_title.Split(",").ToList();
            updateProjectReturn.product_title = product_title;

            return new Result(200, "搜索成功", updateProjectReturn, null);
        }

        public Result updateProjectInfo(UpdateProjectQuery updateProjectQuery)
        {
            Project project = new Project();
            project.product_id = updateProjectQuery.product_id;
            project.product_intro = updateProjectQuery.product_intro;
            project.product_title = "";
            project.product_picture = updateProjectQuery.product_image.Replace("http://localhost:99/", "");
            project.product_price = updateProjectQuery.product_price;
            project.product_notice = updateProjectQuery.product_notice;
            
            foreach (var VARIABLE in updateProjectQuery.product_title)
            {
                if (VARIABLE == "")
                {
                    continue;
                }
                project.product_title =  VARIABLE+","+project.product_title;
            }

            _adminProjectDao.updateProject(project);
            return new Result(200, "更新成功", null, null);
        }
    }
}